package com.ushine.web.base.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ushine.web.base.model.relation.UserGroupAndRoleDO;
import com.ushine.web.base.model.system.UserGroupDO;
import com.ushine.web.base.service.IUserGroupAndRoleService;
import com.ushine.web.base.service.IUserGroupService;
import com.ushine.web.component.http.HttpResponseVO;
import com.ushine.web.component.page.PageRequestQuery;
import com.ushine.web.component.page.PageResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author: yls
 * @Date: 2022/02/16 00:10
 * @Description: 用户组模块
 * @Version 1.0
 */
@RestController
@Api(tags = "基础模块-用户组")
@RequestMapping("system/userGroup")
@Validated
public class UserGroupController {
    @Resource
    private IUserGroupService userGroupService;

    @Resource
    private IUserGroupAndRoleService roleAndUserGroupService;

    @ApiOperation(value = "添加用户组")
    @PostMapping("insertUserGroup")
    public HttpResponseVO<Object> insertUserGroup(@ApiParam(value = "用户组对象", required = true) @RequestBody @Validated UserGroupDO userGroup) {
        UserGroupDO userGroupByName = userGroupService.getUserGroupByName(userGroup.getUserGroupName());
        if (userGroupByName != null) {
            return new HttpResponseVO<>().error("添加失败，用户组名称已存在！");
        }
        Boolean flag = userGroupService.insertUserGroup(userGroup);
        if (Boolean.TRUE.equals(flag)) {
            return new HttpResponseVO<>().ok("用户组添加成功！");
        } else {
            return new HttpResponseVO<>().error("用户组添加失败！");
        }
    }

    @ApiOperation(value = "删除用户组")
    @GetMapping("deleteUserGroup")
    public HttpResponseVO<Object> insertUserGroup(@ApiParam(value = "用户组id", required = true) @RequestParam @Validated
                                                  @NotNull(message = "用户组id不能为空") Long userGroupId) {
        Boolean flag = userGroupService.deleteUserGroup(userGroupId);
        if (Boolean.TRUE.equals(flag)) {
            return new HttpResponseVO<>().ok("用户组删除成功！");
        } else {
            return new HttpResponseVO<>().error("用户组删除失败！");
        }
    }

    @ApiOperation(value = "分页查询用户组")
    @PostMapping("listUserGroupByPage")
    public HttpResponseVO<PageResultVO<UserGroupDO>> listUserGroupByPage(@ApiParam(value = "用户组分页查询对象", required = true)
                                                                         @RequestBody PageRequestQuery<UserGroupDO> pageRequestQuery) {
        Page<UserGroupDO> page = userGroupService.listUserGroups(pageRequestQuery);
        return new HttpResponseVO<PageResultVO<UserGroupDO>>().ok("查询成功！", page);
    }

    @ApiOperation(value = "更新用户组")
    @PostMapping("updateUserGroup")
    public HttpResponseVO<Object> updateUserGroup(@ApiParam(value = "用户组对象", required = true)
                                                  @RequestBody @Validated UserGroupDO userGroup) {
        Boolean flag = userGroupService.updateUserGroup(userGroup);
        if (Boolean.TRUE.equals(flag)) {
            return new HttpResponseVO<>().ok("用户组更新成功！");
        } else {
            return new HttpResponseVO<>().error("用户组更新失败！");
        }
    }

    @ApiOperation(value = "用户组角色设置")
    @PostMapping("setUserGroupRoles")
    public HttpResponseVO<Object> setUserGroupRoles(@ApiParam(value = "用户组角色关联关系", required = true)
                                                    @RequestBody @Validated List<UserGroupAndRoleDO> roleAndUserGroups) {
        Boolean flag = roleAndUserGroupService.insertBatchUserGroupAndRoles(roleAndUserGroups);
        if (Boolean.TRUE.equals(flag)) {
            return new HttpResponseVO<>().ok("用户组角色设置成功！");
        } else {
            return new HttpResponseVO<>().error("用户组角色设置失败！");
        }
    }

}
